package ink.lovejinhu.leetcode;

import org.junit.Test;

import java.security.NoSuchAlgorithmException;

/**
 * @author jinhu
 * created 2021-04-27 8:55
 */
public class Solution_0009 {
    @Test
    public void test() throws NoSuchAlgorithmException {
        int x = 123;
        System.out.println(this.isPalindrome(x));
        x = 121;
        System.out.println(this.isPalindrome(x));
        x = -123;
        System.out.println(this.isPalindrome(x));
        x = 0;
        System.out.println(this.isPalindrome(x));
        x = 10;
        System.out.println(this.isPalindrome(x));
        System.out.println(this.reserve(x));

    }

    /**
     * 负数不可能回文
     * 如果是回文的话，逆置的数也应该原数相等
     *
     * @param x
     * @return
     */
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        } else if (x == 0) {
            return true;
        } else {
            if (this.reserve(x) == 0) {
                return false;
            } else {
                return x == this.reserve(x);
            }
        }

    }

    /**
     * 反转数
     *
     * @param x
     * @return
     */
    public int reserve(int x) {
        long n = 0;
        while (x > 0) {
            n = n * 10 + x % 10;
            x = x / 10;
        }
        return n == (int) n ? (int) n : 0;
    }
}
